package com.zhangdi.dwtools.tree;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.zhangdi.dwtools.collection.FastList;

public class Node {
    public Node() {
        this.children = new FastList<>(Node.class);
        this.allChildren = new FastList<>(Node.class);
    }

    public Node(Node nodeBaseInfo) {
        this.id = nodeBaseInfo.getId();
        this.name = nodeBaseInfo.getName();
        this.pid = nodeBaseInfo.getPid();
        this.lft = nodeBaseInfo.getLft();
        this.rgt = nodeBaseInfo.getLft();

        this.children = new FastList<>(Node.class);
        this.allChildren = new FastList<>(Node.class);
    }

    public Node(long nodeId, String nodeName, long parentId) {
        this.id = nodeId;
        this.name = nodeName;
        this.pid = parentId;

        this.children = new FastList<>(Node.class);
        this.allChildren = new FastList<>(Node.class);
    }

    /**
     * 当前节点
     */
    private long id;
    private String name;
    private long pid;
    private int lft;
    private int rgt;

    private FastList<Node> children;

    @JsonIgnore
    public boolean isChild = false;
    @JsonIgnore
    private FastList<Node> allChildren;

    public long getId() {
        return id;
    }

    public void setId(long id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public long getPid() {
        return pid;
    }

    public void setPid(long pid) {
        this.pid = pid;
    }

    public boolean isChild() {
        return isChild;
    }

    public void setChild(boolean child) {
        isChild = child;
    }

    public void setAllChildren(FastList<Node> allChildren) {
        this.allChildren = allChildren;
    }

    public int getLft() {
        return lft;
    }

    public void setLft(int lft) {
        this.lft = lft;
    }

    public int getRgt() {
        return rgt;
    }

    public void setRgt(int rgt) {
        this.rgt = rgt;
    }

    public FastList<Node> getChildren() {
        return children;
    }

    public void setChildren(FastList<Node> children) {
        this.children = children;
    }

    public void add(Node nodeBaseInfo) {
        this.children.add(nodeBaseInfo);
    }

    public FastList<Node> getAllChildren() {
        if (this.allChildren.isEmpty()) {
            for (Node treeNode : this.children) {
                this.allChildren.add(treeNode);
                this.allChildren.addAll(treeNode.getAllChildren());
            }
        }
        return this.allChildren;
    }
}
